<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组操作</title>
    <script>
        let arr = [
            {id: 1, name: '张三', age: 20},
            {id: 2, name: '李四', age: 19},
            {id: 3, name: '王五', age: 21},
            {id: 4, name: '小六', age: 16}
        ]
        // 计算数组中所有人最大的年龄
        let maxAge = 0
        for(let i = 0; i < arr.length; i ++) {
            maxAge = Math.max(arr[i].age, maxAge)
        }
        console.log(maxAge)
        // 等同于下面代码
        let m = arr.reduce((maxAge, item) => Math.max(maxAge, item.age), 0)
        console.log(m)

        // 数组排序，按照年龄升序排序
        // 形参a，b是每次循环时相邻的2个数组元素
        arr.sort((a, b) => a.age - b.age)
        console.log(...arr)
    </script>
</head>
<body>
    
</body>
</html>